//
//  ShareViewController.m
//  CivilianTravel
//
//  Created by xalo on 16/7/15.
//  Copyright © 2016年 许仕杰. All rights reserved.
//

#import "ShareViewController.h"
#import "shareMessageViewController.h"
#import "headimage.h"
#import <AVFoundation/AVFoundation.h>
#import "ErweimaViewController.h"
#import <AMapSearchKit/AMapSearchKit.h>
#import "PassValue.h"
@interface ShareViewController ()<AVCaptureMetadataOutputObjectsDelegate,AMapSearchDelegate,MAMapViewDelegate>
{
    AMapSearchAPI *_search;
}
@property (weak, nonatomic) IBOutlet UIImageView *imageviewss;
@property (weak, nonatomic) IBOutlet UILabel *weather;
@property (nonatomic,strong)UIView *weatherview;
@property (weak, nonatomic) IBOutlet UILabel *labledays;


@property (weak, nonatomic) IBOutlet UILabel *lablecity;

@property (weak, nonatomic) IBOutlet UILabel *labletemp;


@end


@implementation ShareViewController
//分享
//-(UIView *)weatherview{
//    if (!_weatherview) {
//        _weatherview = [[UIView alloc]initWithFrame:CGRectMake(0, 24, kWidth, kHeight/3)];
//        _weatherview.backgroundColor = [UIColor blueColor];
//            }return _weatherview;
//}
- (void)shareAction{
    shareMessageViewController *share = [[shareMessageViewController alloc] init];
    [self presentViewController:share animated:YES completion:nil];
}

//二维码
- (void)takePhone{
    ErweimaViewController *ervc =[[ErweimaViewController alloc]init];
    [self presentViewController:ervc animated:YES completion:nil];
    
        
}

//签到
- (void)registerAction{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"签到成功" message:@"" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alert show];
}



//更多

- (void)moreAction{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"更多" message:@"" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:nil, nil];
    [alert show];
}

//取消
- (void)cancleAction:(UIButton *)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}




- (void)viewDidLoad {
    [super viewDidLoad];
    [self setFrameWithShare];
    
    NSLog(@"单例正向传值的结果：->%@",[PassValue sharePassValue].aString);
    
    
    
    
//    [self.view addSubview:self.weatherview];
    _search = [[AMapSearchAPI alloc] init];
    _search.delegate = self;
    
    //构造AMapWeatherSearchRequest对象，配置查询参数
    AMapWeatherSearchRequest *request = [[AMapWeatherSearchRequest alloc] init];
    
    if ([PassValue sharePassValue].aString!=nil) {
        request.city=[PassValue sharePassValue].aString;
    }else{
        request.city = @"西安市";
    }
    
    request.type = AMapWeatherTypeLive; //AMapWeatherTypeLive为实时天气；AMapWeatherTypeForecase为预报天气
    
    //发起行政区划查询
    [_search AMapWeatherSearch:request];
 
   
    //  地理编码  显示具体位置
   

   }

- (void)setFrameWithShare{
    NSArray *titleName = @[@"扫一扫",@"签到",@"分享",@"更多"];
    NSArray *imagearray=@[[UIImage imageNamed:@"saoyisao"],[UIImage imageNamed:@"qiandao"],[UIImage imageNamed:@"分享2"],[UIImage imageNamed:@"more2"]];
    CGFloat currentY = [UIScreen mainScreen].bounds.size.height * 0.6;
    CGFloat currentX = [UIScreen mainScreen].bounds.size.width * 0.5;
    
    for (int i = 0; i < titleName.count; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setTitle:titleName[i] forState:UIControlStateNormal];
        [button setImage:imagearray[i] forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:20];
        NSInteger row = i/2;
        NSInteger limit = i%2;
        button.tag = 10000 + i;
        button.frame = CGRectMake(currentX-120 + limit * 160, currentY-150 + row * 160, 80, 80);
        [self.view addSubview:button];
//        button.layer.masksToBounds = YES;
//       button.layer.cornerRadius = 40;
//        button.backgroundColor = [UIColor colorWithRed:47 green:83 blue:132 alpha:0.5];
        [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    }
}

- (void)btnAction:(UIButton *)sender{
    if (sender.tag == 10000) {
        [self takePhone];
    } else if (sender.tag == 10001){
        [self registerAction];
    } else if (sender.tag == 10002){
        [self shareAction];
    } else if (sender.tag == 10003){
        [self moreAction];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
//实现天气查询的回调函数
- (void)onWeatherSearchDone:(AMapWeatherSearchRequest *)request response:(AMapWeatherSearchResponse *)response{
    //如果是实时天气
    if(request.type == AMapWeatherTypeLive)
    {
        if(response.lives.count == 0)
        {
            return;
        }
        AMapLocalWeatherLive *live = (AMapLocalWeatherLive *)response.lives[0];
//        UILabel *showLabel = [[UILabel alloc] init];
//        showLabel.frame = CGRectMake(10, 20, 300, 400);
//        showLabel.numberOfLines = 0;
//        [self.view addSubview:showLabel];
//        
        NSString *showStr = [NSString stringWithFormat:@"地区编码:%@\n省份名:%@\n城市名:%@\n天气现象:%@\n实时温度:%@\n风向:%@\n风力:%@\n空气湿度:%@\n数据发布时间:%@\n",live.adcode,live.province,live.city,live.weather,live.temperature,live.windDirection,live.windPower,live.humidity,live.reportTime];
//        showLabel.text = showStr;
        NSLog(@"%@",showStr);
        self.labledays.text = live.reportTime;
        self.lablecity.text = live.city;
        self.labletemp.text =[NSString stringWithFormat:@"温度:%@" ,live.temperature];
        self.weather.text = live.weather;
        if ([self.weather.text containsString:@"云"]) {
            self.imageviewss.image=[UIImage imageNamed:@"yun"];
        }else if ([self.weather.text containsString:@"晴"]){
            self.imageviewss.image=[UIImage imageNamed:@"sun"];
        }else if ([self.weather.text containsString:@"雨"]){
            self.imageviewss.image=[UIImage imageNamed:@"rain"];
        }else if ([self.weather.text containsString:@"阴"]){
            self.imageviewss.image=[UIImage imageNamed:@"阴天"];
        }
        
    }
    //如果是预报天气
    else
    {
        if(response.forecasts.count == 0)
        {
            return;
        }
        for (AMapLocalWeatherForecast *forecast in response.forecasts) {
        }
    }
    
    
    

    
    
}


@end
